//
//  FJLoginViewController.swift
//  FruitJuice
//
//  Created by Rui on 2023/12/16.
//

import UIKit

class FJLoginViewController: FJBaseViewController {

    private let backView            = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("FJ_SignIn_LoginBg").end()
    private let titleLabel          = c_make_ex(UILabel.self)
        .color(.white).font(31.font_bold)
        .alignment(.center).text("Welcome back").end()
    private let backButton          = c_make_ex(UIButton.self)
        .image("FJ_SignIn_back").end()
    private let emailTextF          = c_make_ex(UITextField.self)
        .bgColor(.black.alpha(0.4)).corner(25).font(17.font_medium)
        .placeholder("Please enter your E-mail").color(.white).end()
    private let passwordTextF       = c_make_ex(UITextField.self)
        .bgColor(.black.alpha(0.4)).corner(25).font(17.font_medium)
        .placeholder("Please enter your password").color(.white).end()
    private let loginButton         = c_make_ex(UIButton.self)
        .bgColor("#FF9133".toColor).corner(25).font(21.font_bold)
        .text("LOGIN").color(.white).end()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigation.bar.isHidden = true
        view.subviews{
            backView.subviews{
                backButton
                titleLabel
                emailTextF
                passwordTextF
                loginButton
            }
        }
        
        backView.fillContainer()
        backButton.top(.statusHeight + 7).leading(15).size(30)
        titleLabel.top(.navi_height + 150.0).centerHorizontally()
        emailTextF.width(320).height(50).centerHorizontally()
        emailTextF.Top == titleLabel.Bottom + 25.5
        passwordTextF.width(320).height(50).centerHorizontally()
        passwordTextF.Top == emailTextF.Bottom + 25
        loginButton.width(320).height(50).centerHorizontally()
        loginButton.Top == passwordTextF.Bottom + 155
        
        backView.isUserInteractionEnabled = true
        emailTextF.style { v in
            v.placehold("#B5B5B5".toColor, font: 17.font_medium)
            let leftImage = UIImageView(image: UIImage(named: "FJ_SignIn_email"))
            leftImage.frame = CGRect(x: 15, y: 13, width: 24, height: 24)
            let leftV = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
            leftV.addSubview(leftImage)
            v.leftView = leftV
            v.leftViewMode = .always
        }
        passwordTextF.style { v in
            v.placehold("#B5B5B5".toColor, font: 17.font_medium)
            let leftImage = UIImageView(image: UIImage(named: "FJ_SignIn_password"))
            leftImage.frame = CGRect(x: 15, y: 13, width: 24, height: 24)
            let leftV = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
            leftV.addSubview(leftImage)
            v.leftView = leftV
            v.leftViewMode = .always
            v.isSecureTextEntry = true
        }
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        loginButton.addTarget(self, action: #selector(clickLoginButton), for: .touchUpInside)
    }

}

//MARK: action
extension FJLoginViewController {
    
    @objc private func clickBackButton(){
        
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc private func clickLoginButton(){
        
        guard let email = emailTextF.text, email.isValidEmail else {
            "Invalid email address.".toast_error
            return
        }
        guard let password = passwordTextF.text, password.validatePassword() else{
             "The password should be 6-14 digits.".toast_error
            return
        }
        RT.appleLogininWith(emailNum: email, Password: password) { info in
            let str = GetInfo(key: "UserInfo") as! NSDictionary
            let jsonData = InfoUtil.initInfo.deserialize(from: str)
            USEINFOMODEL = jsonData!
            UIApplication.window?.rootViewController = FJNavViewController(rootViewController:  FJHomeViewController())
        }
    }
    
}
